import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jetour_line/base_widget/base_image.dart';
import 'package:jetour_line/base_widget/image_network_widget.dart';
import 'package:jetour_line/base_widget/sliver_list_widget.dart';
import 'package:jetour_line/map/map_utils.dart';
import 'package:jetour_line/my_colors.dart';
import 'package:jetour_line/pages/line/lineDetail/line_detali_image_page.dart';
import 'package:jetour_line/utils/aha_utils.dart';
import 'package:jetour_line/utils/extension_int.dart';
import 'package:jetour_line/utils/navi_go_utils.dart';
import 'package:jetour_line/utils/page_push_utils.dart';
import '../../../base_widget/my_alert.dart';
import '../../../model/line_detail_model.dart';

typedef _BuyLineCallBack = void Function();

class LineDetailTripListWidget extends StatefulWidget {
  final LineDetailModel lineDetailModel;
  final bool selTrip; //true 行程 false 介绍
  final _BuyLineCallBack callBack;

  const LineDetailTripListWidget(
      {Key? key,
      required this.lineDetailModel,
      required this.selTrip,
      required this.callBack})
      : super(key: key);

  @override
  State<LineDetailTripListWidget> createState() =>
      _LineDetailTripListWidgetState();
}

class _LineDetailTripListWidgetState extends State<LineDetailTripListWidget> {
  int selIndex = 0;
  bool neadBuy = false;

  @override
  Widget build(BuildContext context) {
    int? count = widget.lineDetailModel.lineData?.length;
    neadBuy = widget.lineDetailModel.needBuy!;

    return SliverListWidget.getSliverList(count!, (context, index) {
      return Container(
          color: Colors.white,
          child: widget.selTrip ? groupTripWidget(index) : groupWidget(index));
    });
  }

  Widget groupTripWidget(int groupIndex) {
    LineDatum? datum = widget.lineDetailModel.lineData![groupIndex];
    String day = 'DAY ${groupIndex + 1}';
    String? startCity = datum?.startCityName;
    String? endCity = datum?.endCityName;
    String? dis = '约${datum?.totalDistance}km';
    bool sel = selIndex == groupIndex;
    int? childCount = datum?.node?.length;
    return Container(
      color: MyColors.white,
      child: Column(
        children: [
          Padding(
            padding: EdgeInsets.only(left: 24.px, right: 24.px),
            child: ClipRRect(
                borderRadius: BorderRadius.all(Radius.circular(8.px)),
                child: GestureDetector(
                  onTap: () {
                    if (neadBuy) {
                      return;
                    }
                    if (selIndex == groupIndex) {
                      selIndex = -1;
                    } else {
                      selIndex = groupIndex;
                    }
                    setState(() {});
                  },
                  child: Container(
                      color: MyColors.colorF1F1F1,
                      width: double.infinity,
                      height: 96.px,
                      child: Row(
                        children: [
                          SizedBox(
                            width: 20.px,
                          ),
                          Text(
                            day,
                            style: TextStyle(
                                fontSize: 30.px,
                                color: MyColors.color333333,
                                fontWeight: FontWeight.w500),
                          ),
                          SizedBox(
                            width: 16.px,
                          ),
                          Text(
                            startCity!,
                            style: TextStyle(
                                fontSize: 30.px,
                                color: MyColors.color333333,
                                fontWeight: FontWeight.w500),
                          ),
                          Padding(
                            padding: EdgeInsets.only(
                              left: 8.px,
                              right: 8.px,
                            ),
                            child: BaseImage(
                              image: 'lsgj_icon_cityline',
                              width: 32.px,
                              height: 32.px,
                            ),
                          ),
                          Expanded(
                            child: Text(
                              endCity!,
                              style: TextStyle(
                                  fontSize: 30.px,
                                  color: MyColors.color333333,
                                  fontWeight: FontWeight.w500),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                          SizedBox(
                            width: 16.px,
                          ),
                          Text(
                            dis,
                            style: TextStyle(
                              fontSize: 28.px,
                              color: MyColors.color666666,
                            ),
                          ),
                          BaseImage(
                            image: sel ? 'lsgj_btn_putaway' : 'lsgj_btn_open',
                            width: 32.px,
                            height: 32.px,
                          ),
                          SizedBox(
                            width: 16.px,
                          ),
                        ],
                      )),
                )),
          ),
          Container(
            width: double.infinity,
            height: 15.px,
          ),
          if (sel)
            for (int i = 0; i < childCount!; i++)
              widget.selTrip
                  ? childTripWidget(groupIndex, i)
                  : childWidhet(groupIndex, i)
        ],
      ),
    );
  }

  Widget childTripWidget(int groupIndex, int childIndex) {
    LineDatum? datum = widget.lineDetailModel.lineData![groupIndex];
    LineDatumNode? node = datum?.node![childIndex];
    bool showShadow = false;
    if (neadBuy && groupIndex == 0) {
      int? len = datum?.node!.length;
      if (childIndex == len! - 2) {
        showShadow = true;
      }
      if (childIndex == len - 1) {
        String price = '￥${widget.lineDetailModel.salePrice!} 查看完整路书';
        return GestureDetector(
          onTap: buyLine,
          child: Container(
            width: double.infinity,
            height: 150.px,
            color: MyColors.white,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  price,
                  style: TextStyle(
                      fontSize: 30.px,
                      color: MyColors.colorEE7219,
                      fontWeight: FontWeight.w500),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
              ],
            ),
          ),
        );
      }
    }
    bool showTopLine = true;
    bool showBottomLine = true;
    String? time = node?.arriveTime;
    String typeImg = AhaUtils.getImagePath('ls_icon_travelwh'); //行
    //1、酒店 2、景点 3、餐饮 4、其他(行程点)
    if (node?.type == 1) {
      typeImg = AhaUtils.getImagePath('lsgj_btn_livepre'); //住
    } else if (node?.type == 2) {
      typeImg = AhaUtils.getImagePath('lsgj_btn_playpre'); //玩
    } else if (node?.type == 3) {
      typeImg = AhaUtils.getImagePath('lsgj_btn_eatye'); //吃
    } else {
      typeImg = AhaUtils.getImagePath('ls_icon_travelwh'); //行
    }
    String? caption = node?.caption;
    String? disTime = '${node?.distanceText} 行驶${node?.durationText}';

    int? len = datum?.node?.length;
    String? endText;
    if (childIndex == (len! - 1)) {
      endText = "第${groupIndex + 1}天行程介绍";
    }

    return Stack(
      children: [
        Padding(
          padding: EdgeInsets.only(left: 24.px, right: 24.px),
          child: Column(
            children: [
              Row(
                children: [
                  Container(
                    padding: EdgeInsets.only(right: 8.px),
                    alignment: Alignment.centerRight,
                    width: 112.px,
                    height: 100.px,
                    child: Text(
                      time!,
                      style: TextStyle(
                          fontSize: 30.px, color: MyColors.color999999),
                    ),
                  ),
                  Column(
                    children: [
                      showTopLine
                          ? Container(
                              width: 1.px,
                              height: 30.px,
                              color: MyColors.colorF1F1F1,
                            )
                          : SizedBox(
                              height: 30.px,
                            ),
                      Image(
                        image: AssetImage(typeImg),
                        width: 40.px,
                        height: 40.px,
                      ),
                      showBottomLine
                          ? Container(
                              width: 1.px,
                              height: 30.px,
                              color: MyColors.colorF1F1F1,
                            )
                          : SizedBox(
                              height: 30.px,
                            ),
                    ],
                  ),
                  SizedBox(
                    width: 8.px,
                  ),
                  Expanded(
                    child: Text(
                      caption!,
                      style: TextStyle(
                          fontSize: 30.px,
                          color: MyColors.color333333,
                          fontWeight: FontWeight.w500),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                  GestureDetector(
                    onTap: () {
                      goNavi(groupIndex, childIndex);
                    },
                    child: Container(
                      color: Colors.white,
                      width: 88.px,
                      height: 100.px,
                      alignment: Alignment.center,
                      child: BaseImage(
                        image: 'ls_btn_nav',
                        width: 40.px,
                        height: 40.px,
                      ),
                    ),
                  ),
                ],
              ),
              Row(
                children: [
                  SizedBox(
                    width: 112.px,
                  ),
                  BaseImage(
                    image: 'lsgj_icon_car',
                    width: 40.px,
                    height: 40.px,
                  ),
                  Text(
                    endText ?? disTime,
                    style:
                        TextStyle(fontSize: 24.px, color: MyColors.color999999),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ],
              )
            ],
          ),
        ),
        if (showShadow)
          Container(
              width: double.infinity,
              height: 150.px,
              decoration: const BoxDecoration(
                  gradient: LinearGradient(
                      begin: Alignment.topCenter, //渐变开始于上面的中间开始
                      end: Alignment.bottomCenter, //渐变结束于下面的中间
                      colors: [
                    Color(0x00ffffff),
                    Color(0xFFFFFFFF)
                  ] //开始颜色和结束颜色])),
                      )))
      ],
    );
  }

  Widget groupWidget(int groupIndex) {
    String day = 'Day${groupIndex + 1}';
    LineDatum? datum = widget.lineDetailModel.lineData![groupIndex];
    int? childCount = datum?.node?.length;
    return Padding(
      padding: EdgeInsets.only(left: 24.px, right: 24.px),
      child: Column(
        children: [
          if (groupIndex == 0) titleWidget(),
          ClipRRect(
            borderRadius: BorderRadius.all(Radius.circular(4.px)),
            child: Container(
                alignment: Alignment.centerLeft,
                width: double.infinity,
                height: 90.px,
                color: MyColors.colorF1F1F1,
                child: Padding(
                  padding: EdgeInsets.only(left: 25.px),
                  child: Text(
                    day,
                    style: TextStyle(
                        fontSize: 40.px,
                        color: MyColors.color333333,
                        fontWeight: FontWeight.w500),
                  ),
                )),
          ),
          for (int i = 0; i < childCount!; i++) childWidhet(groupIndex, i)
        ],
      ),
    );
  }

  String getPlayTime() {
    String str = "";

    for (int i = 0; i < widget.lineDetailModel.playSeason!.length; i++) {
      PlaySeason? season = widget.lineDetailModel.playSeason?[i];
      if (i == 0) {
        str = '${season?.start}月~${season?.end}月';
      } else {
        str = '$str,${season?.start}月~${season?.end}月';
      }
    }
    return str;
  }

  Widget titleWidget() {
    String playTime = getPlayTime();
    return Padding(
      padding: EdgeInsets.only(top: 0.px),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              BaseImage(
                image: 'ls_icon_ywsj',
                width: 40.px,
                height: 40.px,
              ),
              SizedBox(
                width: 13.px,
              ),
              Text(
                '最佳游玩季节',
                style: TextStyle(
                    fontSize: 34.px,
                    color: MyColors.color333333,
                    fontWeight: FontWeight.w500),
              ),
            ],
          ),
          SizedBox(
            height: 20.px,
          ),
          Text(
            playTime,
            style: TextStyle(
              fontSize: 34.px,
              color: MyColors.color666666,
            ),
          ),
          SizedBox(
            height: 40.px,
          ),
          Row(
            children: [
              BaseImage(
                image: 'ls_icon_rcap',
                width: 40.px,
                height: 40.px,
              ),
              SizedBox(
                width: 13.px,
              ),
              Text(
                '日程安排',
                style: TextStyle(
                    fontSize: 34.px,
                    color: MyColors.color333333,
                    fontWeight: FontWeight.w500),
              ),
            ],
          ),
          SizedBox(
            height: 30.px,
          )
        ],
      ),
    );
  }

  Widget childWidhet(int groupIndex, int childIndex) {
    LineDatum? datum = widget.lineDetailModel.lineData![groupIndex];
    LineDatumNode? node = datum?.node![childIndex];
    String? caption = node?.caption;
    String? address = node?.address;
    List<String?>? images = node?.images;
    bool showShadow = false;
    if (neadBuy) {
      if (groupIndex == 0) {
        int? len = datum?.node!.length;
        if (childIndex == len! - 2) {
          showShadow = true;
        }
        if (childIndex == len - 1) {
          String price = '￥${widget.lineDetailModel.salePrice!} 查看完整路书';
          return GestureDetector(
            onTap: buyLine,
            child: Container(
              width: double.infinity,
              height: 150.px,
              color: MyColors.white,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    price,
                    style: TextStyle(
                        fontSize: 30.px,
                        color: MyColors.colorEE7219,
                        fontWeight: FontWeight.w500),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ],
              ),
            ),
          );
        }
      } else {
        return SizedBox();
      }
    }

    String type; //1、酒店 2、景点 3、餐饮 4、其他(行程点)
    if (node?.type == 1) {
      type = '酒店';
    } else if (node?.type == 2) {
      type = '景点';
    } else if (node?.type == 3) {
      type = '餐饮';
    } else {
      type = '行程';
    }
    String? des = node?.durationText;
    String? guideline = node?.userDesc?.say;
    return Stack(
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 30.px,
            ),
            Row(
              children: [
                ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(23.px)),
                  child: Container(
                    alignment: Alignment.center,
                    width: 80.px,
                    height: 46.px,
                    color: MyColors.colorF1F1F1,
                    child: Text(
                      type,
                      style: TextStyle(
                          fontSize: 28.px, color: MyColors.color666666),
                    ),
                  ),
                ),
                SizedBox(
                  width: 10.px,
                ),
                Text(
                  caption!,
                  style: TextStyle(
                      fontSize: 34.px,
                      color: MyColors.color333333,
                      fontWeight: FontWeight.w500),
                ),
              ],
            ),
            imageWidget(images),
            SizedBox(
              height: 30.px,
            ),
            SizedBox(
              height: 50.px,
              child: Text(
                '避坑攻略',
                style: TextStyle(
                  fontSize: 30.px,
                  color: MyColors.color333333,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
            SizedBox(
              height: 15.px,
            ),
            if (guideline != null)
              Text(
                guideline,
                style: TextStyle(fontSize: 30.px, color: MyColors.color666666),
              ),
            SizedBox(
              height: 25.px,
            ),
            GestureDetector(
              onTap: () {
                goNavi(groupIndex, childIndex);
              },
              child: Row(
                children: [
                  Opacity(
                    opacity: 0.5,
                    child: BaseImage(
                      image: 'icon_addressblack',
                      width: 40.px,
                      height: 40.px,
                    ),
                  ),
                  SizedBox(
                    width: 4.px,
                  ),
                  Expanded(
                    child: Text(
                      address!,
                      style: TextStyle(
                          fontSize: 28.px, color: MyColors.color999999),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: 45.px,
            ),
            Container(
              width: double.infinity,
              height: 1.px,
              color: MyColors.colorF1F1F1,
            )
          ],
        ),
        if (showShadow)
          Positioned(
              left: 0,
              right: 0,
              bottom: 0,
              child: Container(
                  height: 150.px,
                  decoration: const BoxDecoration(
                      gradient: LinearGradient(
                          begin: Alignment.topCenter, //渐变开始于上面的中间开始
                          end: Alignment.bottomCenter, //渐变结束于下面的中间
                          colors: [
                        Color(0x00ffffff),
                        Color(0xFFFFFFFF)
                      ] //开始颜色和结束颜色])),
                          ))))
      ],
    );
  }

  Widget imageWidget(List<String?>? images) {
    if (images!.isEmpty) {
      return SizedBox();
    }
    int leng = images.length;
    return GestureDetector(
      onTap: () {
        PagePushUtils.push(context, LineDetailImageScreen(images: images));
      },
      child: Container(
        margin: EdgeInsets.only(top: 30.px),
        width: double.infinity,
        child: Column(
          children: [
            Row(
              children: [
                Expanded(
                    child: ImageNetworkWidget(
                  images[0]!,
                  height: 150.px,
                )),
                SizedBox(
                  width: 30.px,
                ),
                Expanded(
                    child: leng < 2
                        ? const SizedBox()
                        : ImageNetworkWidget(
                            images[1]!,
                            height: 150.px,
                            radius: 10.px,
                          )),
                SizedBox(
                  width: 30.px,
                ),
                Expanded(
                    child: (leng < 3 || leng == 4)
                        ? const SizedBox()
                        : ImageNetworkWidget(
                            images[2]!,
                            radius: 10.px,
                            height: 150.px,
                          )),
              ],
            ),
            if (leng > 3)
              Padding(
                padding: EdgeInsets.only(top: 30.px),
                child: Row(
                  children: [
                    Expanded(
                        child: ImageNetworkWidget(
                      images[leng == 4 ? 2 : 3]!,
                      radius: 10.px,
                      height: 150.px,
                    )),
                    SizedBox(
                      width: 30.px,
                    ),
                    Expanded(
                        child: leng < 4
                            ? const SizedBox()
                            : ImageNetworkWidget(
                                images[leng == 4 ? 3 : 4]!,
                                radius: 10.px,
                                height: 150.px,
                              )),
                    SizedBox(
                      width: 30.px,
                    ),
                    Expanded(
                        child: (leng < 6)
                            ? const SizedBox()
                            : ImageNetworkWidget(
                                images[5]!,
                                radius: 10.px,
                                height: 150.px,
                              )),
                  ],
                ),
              )
          ],
        ),
      ),
    );
  }

  void buyLine() {
    widget.callBack();
  }

  void goNavi(int groupIndex, int childIndex) {
    LineDatum? datum = widget.lineDetailModel.lineData![groupIndex];
    LineDatumNode? node = datum?.node![childIndex];
    // NaviGoUtils.goNaiv(node!.lat, node.lng, node.caption);
    List<String> hasMaps = MapUtils.hasMaps;
    if (hasMaps != null && hasMaps.isNotEmpty) {
      showMyBottomSheet(context, hasMaps, (index) {
        double? lat = node?.lat;
        double? lng = node?.lng;
        String? caption = node?.caption;
        MapUtils.naviGo(lat, lng, caption, hasMaps[index]);
      });
    }
  }
}
